<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avator">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef">
        <el-form-item prop="email" class="mt pd">
          <el-input
            v-model="loginForm.email"
            prefix-icon="iconfont icon-user"
            type="email"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" class="pd">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="pd btnItem">
          <el-button type="danger" @click="reset">重置</el-button>
          <el-button type="primary" @click="login">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    const emailValidate = (rule, value, cb) => {
      const emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
      if (emailReg.test(value)) {
        // 如果通过验证，return cb()
        return cb()
      }
      // 否则在cb里面new 一个 Error，传入错误信息
      cb(new Error('邮箱格式不符合规则，请重新输入'))
    }
    return {
      // 登陆表单对象
      loginForm: {
        email: 'dg70946@163.com',
        password: '123'
      },
      // 登陆表单验证规则
      loginRules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { min: 3, max: 16, message: '请输入正确的邮箱地址', trigger: 'blur' },
          { validator: emailValidate, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 16,
            message: '请输入3-16位字符的密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {},
  methods: {
    login() {
      // 预验证
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('验证不通过')
        }
        // 验证通过，发送请求
        const { data: res } = await this.$http.post('/login', this.loginForm)
        console.log(res)
        // 判断请求结果
        if (res.meta.status !== 200) {
          return this.$message.error('登陆失败，账号或密码错误')
        }

        this.saveData(res)

        this.$message.success('登陆成功')
        // 路由跳转
        this.$router.push('/home')
      })
    },
    reset() {
      this.$refs.loginFormRef.resetFields()
    },
    saveData(res) {
      // 保存数据到vuex中
      this.$store.commit('saveUname', res.data.user.username)
      this.$store.commit('saveUid', res.data.user._id)
      this.$store.commit('saveUrole', res.data.user.role)
      this.$store.commit('saveAvator', 'http://127.0.0.1' + res.data.user.avator)

      // 保存数据到本地存储中
      window.localStorage.setItem('uid', res.data.user._id)
      window.localStorage.setItem('token', res.data.token)
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
  .login_box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 4px;
    .avator {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      border-radius: 50%;
      background-color: #fff;
      padding: 10px;
      border: 1px solid #eee;
      box-shadow: 0 0 15px #ddd;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .mt {
      margin-top: 120px;
    }
    .pd {
      padding: 0 20px;
    }
    .btnItem {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
